package com.lz.ovuola.web.api;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.lz.ovuola.service.UserService;
import com.lz.ovuola.util.annotation.ApiRestController;
import com.lz.ovuola.util.threadlocal.UserContextHolder;

/**
 * 用户控制层
 * 
 * @author fz
 *
 */
@RestController
@RequestMapping("/users/")
public class UserController extends AbstractRestController {

	@Resource
	private UserService userService;

	/**
	 * 手机号注册
	 * 
	 * @param userCommand
	 * @return
	 */
	// @ApiOperation(value = "手机号注册", notes = "")
	@ApiRestController(isRight = false)
	@RequestMapping(value = "register", method = RequestMethod.POST)
	public String save(@RequestBody UserCommand userCommand) {

		return userService.save(userCommand.getUserName(),
				userCommand.getPassword(), userCommand.getNickName(),
				userCommand.getCode(), userCommand.getMark(),
				userCommand.getDevice());
	}

	/**
	 * 绑定设备
	 * 
	 * @param hardwareEquipment
	 *            设备编码
	 * @return
	 */
	@ApiRestController(isRight = true)
	@RequestMapping(value = "hardwareEquipment", method = RequestMethod.POST)
	public String saveHardwareEquipment(@RequestBody UserCommand userCommand) {
		// 返回数据
		return userService.saveHardwareEquipment(userCommand
				.getHardwareEquipment(), UserContextHolder.get().getUserId());
	}

	/**
	 * 解绑设备
	 * 
	 * @return
	 */
	@ApiRestController(isRight = true)
	@RequestMapping(value = "hardwareEquipment", method = RequestMethod.DELETE)
	public String deleteHardwardEquipment() {
		return userService.deleteHardwareEquipment(UserContextHolder.get()
				.getUserId());
	}

	/**
	 * 修改昵称
	 * 
	 * @param userCommand
	 * @return
	 */
	@ApiRestController(isRight = true)
	@RequestMapping(value = "nickName", method = RequestMethod.PUT)
	public String updateNickName(@RequestBody UserCommand userCommand) {
		return userService.updateNickName(userCommand.getNickName(),
				UserContextHolder.get().getUserId());
	}

	/**
	 * 修改头像
	 * 
	 * @param headImage
	 * @return
	 * @throws Exception
	 * 
	 */
	@ApiRestController(isRight = true)
	@RequestMapping(value = "headImage", method = RequestMethod.POST)
	public String updateHeadImage(MultipartFile headImage) throws Exception {
		return userService.updateHeadImage(headImage.getBytes(),
				headImage.getContentType());

	}
}
